package com.example.locachat;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ChatActivity extends Activity implements OnClickListener {

    private List<ListData> lists;
    private ListView lv;
    private EditText sendtext;
    private Button send_btn;
    private Button close_btn;
    private String content_str;
    private TextAdapter adapter;
    private String[] welcome_array;
    // 做比对时间；老时间
    private double currentTime = 0, oldTime = 0;
    TreeDBDao mDBDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        initView();
    }

    private void initView() {
        lv = (ListView) findViewById(R.id.lv);
        sendtext = (EditText) findViewById(R.id.sendText);
        send_btn = (Button) findViewById(R.id.send_btn);
        close_btn= (Button) findViewById(R.id.bt_close_chat);
        lists = new ArrayList<ListData>();

        mDBDao = new TreeDBDao(ChatActivity.this);//实例化对象
        mDBDao.openDataBase();//打开数据库

        send_btn.setOnClickListener(this);
        close_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        adapter = new TextAdapter(lists, this);
        lv.setAdapter(adapter);
        ListData listData;
        //初始化时间以及欢迎语
        listData = new ListData(getRandomWelcomeTips(), ListData.RECEIVER,
                getTime());
        lists.add(listData);

        referrerData(listData);
    }

    private void referrerData(ListData listData) {
        List<ListData> listDatalistData =
                mDBDao.queryDataList();
        Log.d("ChatActivity", "listData:" + listData);
        if (listData != null) {
            listDatalistData.add( listData);
        }

        lists.clear();
        lists.addAll(listDatalistData);
        Log.d("ChatActivity", "listData:" + listData);
        adapter.notifyDataSetChanged();

    }

    /**
     * 用户第一次进入，随机获取欢迎语
     */
    private String getRandomWelcomeTips() {


        return this.getResources()
                .getString(R.string.welecome_tip);
    }


    @Override
    public void onClick(View v) {
        getTime();
        content_str = sendtext.getText().toString();
        sendtext.setText("");
        // 去掉空格
        String dropk = content_str.replace(" ", "");
        // 去掉回车

        /**
         * 不能发送空白
         */
        if(!TextUtils.isEmpty(content_str)) {
            ListData listData;
            /**
             * 吧发送出去的文字加入聊天里边
             */
            listData = new ListData(content_str, ListData.SEND, getTime());
            lists.add(listData);
            mDBDao.insertData(listData);
            String back = "";

            /**
             * 回复逻辑
             */
            if (content_str.contains("0")) {
                back = "你选择了0";

            } else if (content_str.contains("1")) {
                back = "你选择了1";
            } else if (content_str.contains("2")) {
                back = "你选择了2";
            } else if (content_str.contains("3")) {
                back = "你选择了3";
            } else if (content_str.contains("4")) {
                back = "你选择了4";
            } else if (content_str.contains("5")) {
                back = "你选择了5";
            } else if (content_str.contains("6")) {
                back = "你选择了6";
            } else if (content_str.contains("7")) {
                back = "你选择了7";
            }
            /**
             * 吧发送出去的文字加入聊天里边
             */
            listData = new ListData(back, ListData.RECEIVER, getTime());
            lists.add(listData);
            mDBDao.insertData(listData);
            /**
             * 更新数据库
             */
            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 获取时间
     */
    private String getTime() {
        currentTime = System.currentTimeMillis();
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date curDate = new Date();
        String str = format.format(curDate);
        // 如果超过5分钟.
        if (currentTime - oldTime >= 5 * 60 * 1000) {
            oldTime = currentTime;
            return str;
        } else {
            return "";
        }

    }
}